from app import app, db, License
from datetime import datetime, timedelta

def init_database():
    """初始化数据库"""
    with app.app_context():
        # 创建所有表
        db.create_all()
        
        # 创建测试license（可选）
        test_license = License(
            license_key='TEST-1234-5678-9ABC-DEF0-1234-5678-9ABC',
            user_name='测试用户',
            user_email='test@example.com',
            expire_at=datetime.utcnow() + timedelta(days=30),
            max_activations=1
        )
        
        # 检查是否已存在
        existing = License.query.filter_by(license_key=test_license.license_key).first()
        if not existing:
            db.session.add(test_license)
            db.session.commit()
            print(f"测试License已创建: {test_license.license_key}")
        else:
            print("测试License已存在")
        
        print("数据库初始化完成")

if __name__ == '__main__':
    init_database()